package com.hollysys.hollimsa.biz.istio.virtualservice.domain;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;

@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "创建虚拟服务必要的参数")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class VirtualserviceVO extends VirtualserviceAdditionalVO {


    @ApiModelProperty(value = "服务域名  参考istio官方地址 https://istio.io/latest/zh/docs/reference/config/networking/virtual-service/#VirtualService#hosts描述 ", required = true)
    @Size(min = 1, message = "至少要有一个host")
    /** 服务域名 */
    private String[] hosts;

    @ApiModelProperty(value = "网关")
    /** 网关 */
    private String[] gateways;

    @ApiModelProperty(value = "路由项")
    @Valid
    /** 路由项 */
    private HttpRoute[] http;


    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class HttpRoute implements Serializable {

        private static final long serialVersionUID = -2465956025727447646L;
        @ApiModelProperty(value = "匹配要激活的规则要满足的条件")
        private HTTPMatchRequest[] match;
        @ApiModelProperty(value = "http规则可以重定向或转发（默认）流量")
        @Valid
        private HTTPRouteDestination[] route;
    }


    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class HTTPMatchRequest implements Serializable {

        private static final long serialVersionUID = 6168586555566956850L;
        @ApiModelProperty(value = "路由条件 uri")
        private String routeCondition;
        @ApiModelProperty(value = "匹配类型 exact prefix regex")
        private String matchType;
        @ApiModelProperty(value = "规则字符串")
        private String inputRule;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class HTTPRouteDestination implements Serializable {
        private static final long serialVersionUID = 7347440699609734504L;
        @ApiModelProperty(value = "Destination唯一标识应将请求/连接转发到的服务实例", required = true)
        @NotNull
        @Valid
        private Destination destination;
        @ApiModelProperty(value = "要转发到服务版本的流量比例。（0-100）。各个目标之间的权重之和应为==100。如果规则中只有一个目标，则权重值将假定为100")
        private Integer weight;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class Destination implements Serializable {
        private static final long serialVersionUID = -9141496388800705825L;
        @ApiModelProperty(value = "服务注册表中的服务名称。从平台的服务注册表（例如Kubernetes服务，Consul服务等）", required = true)
        @NotEmpty
        private String host;
        @ApiModelProperty(value = "服务中子集的名称。仅适用于网格内的服务。该子集必须在相应的DestinationRule中定义")
        private String subset;
        @ApiModelProperty(value = "指定要寻址的主机上的端口。如果服务仅公开单个端口，则不需要显式选择端口")
        private PortSelector port;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class PortSelector implements Serializable {
        private static final long serialVersionUID = 8278620937558290811L;
        @ApiModelProperty(value = "指定用于匹配或选择最终路由的端口号")
        private Integer number;
    }
}
